//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartMultiplePiesView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartMultiplePiesView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartMultiplePiesView

IMPLEMENT_DYNCREATE(CChartMultiplePiesView, CBCGPChartExampleView)

CChartMultiplePiesView::CChartMultiplePiesView()
	: CBCGPChartExampleView(CChartMultiplePiesView::IDD)
{
	//{{AFX_DATA_INIT(CChartMultiplePiesView)
	m_bShowDataLabels = TRUE;
	m_nDataLabelPosition = 0;
	m_bExplodePie = FALSE;
	m_nSeriesNamePosition = 1;
	//}}AFX_DATA_INIT
}

CChartMultiplePiesView::~CChartMultiplePiesView()
{
}

void CChartMultiplePiesView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartMultiplePiesView)
	DDX_Control(pDX, IDC_EXPLODED_PIE, m_wndExplodePie);
	DDX_Control(pDX, IDC_SERIES_NAME_POSITION, m_wndSeriesNamePosition);
	DDX_Control(pDX, IDC_DATA_LABEL_POSITION, m_wndDataLabelPosition);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_POSITION, m_nDataLabelPosition);
	DDX_Check(pDX, IDC_EXPLODED_PIE, m_bExplodePie);
	DDX_CBIndex(pDX, IDC_SERIES_NAME_POSITION, m_nSeriesNamePosition);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartMultiplePiesView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartMultiplePiesView)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_LABEL_POSITION, OnUpdateChart)
	ON_BN_CLICKED(IDC_EXPLODED_PIE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_SERIES_NAME_POSITION, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartMultiplePiesView diagnostics

#ifdef _DEBUG
void CChartMultiplePiesView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartMultiplePiesView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartMultiplePiesView message handlers

void CChartMultiplePiesView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}
	
	CBCGPInfoTipOptions infoTipOptions;
	infoTipOptions.m_StemLocation = CBCGPPopupWindow::BCGPPopupWindowStemLocation_Left;
	
	m_wndChart.EnableInfoTip(TRUE, &infoTipOptions);
	
	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->SetChartTitle(_T("Product Sales by Year:"));

	int nTotalYears = 5;

	switch (m_Feature)
	{
	case CDemoFeature::BCGP_MultiplePies:
		pChart->SetChartType(BCGPChartPie);
		break;

	case CDemoFeature::BCGP_DoughnutNested:
		pChart->SetChartType(BCGPChartDoughnutNested);
		nTotalYears = 3;
		m_nSeriesNamePosition = 0;
		m_wndSeriesNamePosition.EnableWindow(FALSE);
		m_wndExplodePie.EnableWindow(FALSE);

		UpdateData(FALSE);
		break;
	}

	for (int nYear = 0; nYear < nTotalYears; nYear++)
	{
		pChart->AddChartData(_T("Product A"), Rand(10000, 50000), nYear);
		pChart->AddChartData(_T("Product B"), Rand(10000, 50000), nYear);
		pChart->AddChartData(_T("Product C"), Rand(10000, 50000), nYear);
		pChart->AddChartData(_T("Product D"), Rand(10000, 50000), nYear);
		pChart->AddChartData(_T("Product E"), Rand(10000, 50000), nYear);
		pChart->AddChartData(_T("Product F"), Rand(10000, 50000), nYear);
		pChart->AddChartData(_T("Product G"), Rand(10000, 50000), nYear);

		pChart->SetSeriesName(m_arYears[nTotalYears - nYear - 1], nYear);
	}

	pChart->SetThemeOpacity(70);
	m_bIsReady = TRUE;

	OnUpdateChart();
}

void CChartMultiplePiesView::OnUpdateChart() 
{
	UpdateData();
	
	m_wndDataLabelPosition.EnableWindow(m_bShowDataLabels);
	
	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);
	
	pChart->ShowDataLabels(m_bShowDataLabels);

	if (m_bShowDataLabels)
	{
		BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
		dataLabelOptions.m_position = (BCGPChartDataLabelOptions::LabelPosition)m_nDataLabelPosition;
		dataLabelOptions.m_bUnderlineDataLabel = TRUE;

		if (m_Feature != CDemoFeature::BCGP_DoughnutNested)
		{
			dataLabelOptions.m_bDrawDataLabelBorder = dataLabelOptions.m_position != BCGPChartDataLabelOptions::LP_DEFAULT_POS && dataLabelOptions.m_position != BCGPChartDataLabelOptions::LP_OUTSIDE_END;
			dataLabelOptions.m_content = BCGPChartDataLabelOptions::LC_PERCENTAGE;
		}
		else
		{
			dataLabelOptions.m_bDrawDataLabelBorder = TRUE;
			dataLabelOptions.m_content = (BCGPChartDataLabelOptions::LabelContent)(BCGPChartDataLabelOptions::LC_PERCENTAGE | BCGPChartDataLabelOptions::LC_SERIES_NAME);
		}
		
		pChart->SetDataLabelsOptions(dataLabelOptions);
	}
	
	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartPieSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartPieSeries, pChart->GetSeries(i));
		if (pSeries != NULL)
		{
			ASSERT_VALID(pSeries);
			
			pSeries->SetDefaultFillGradientType(CBCGPBrush::BCGP_GRADIENT_BEVEL);
			
			pSeries->SetPieExplosion(m_bExplodePie ? 5 : 0);
			pSeries->SetDataPointPieExplosion(m_bExplodePie ? 10 : 0, 0);

			pSeries->SetTileCaption((CBCGPChartPieSeries::BCGP_CHART_TILE_CAPTION_POSITION)m_nSeriesNamePosition);

			CBCGPChartDoughnutSeries* pDoughnutSeries = DYNAMIC_DOWNCAST(CBCGPChartDoughnutSeries, pSeries);
			if (pDoughnutSeries != NULL)
			{
				ASSERT_VALID(pDoughnutSeries);
				pDoughnutSeries->SetDoughnutPercent(30);
			}
		}
	}
	
	pChart->Redraw();
}
